package com.ocm.product.service.impl;

import com.alibaba.fastjson.JSON;
import com.ocm.core.log.exception.ServiceException;
import com.ocm.core.tool.utils.BeanUtil;
import com.ocm.core.tool.utils.StringUtil;
import com.ocm.product.dto.CartUpdateDTO;
import com.ocm.product.rs.CartRS;
import com.ocm.product.service.ICartService;
import com.ocm.product.service.ISkuService;
import com.ocm.product.vo.CartItemVO;
import com.ocm.product.vo.CartShopVO;
import com.ocm.product.vo.CartVO;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

import static com.ocm.common.cache.CacheNames.CART_KEY;

/**
 * @Author hqy
 * @Date 13/01/2022
 * @Description
 */
@Service
public class CartServiceImpl implements ICartService {

	@Resource
	private ISkuService skuService;


	@Override
	public Long countCart(Long userId) {
		return getCardRedisOps(userId).size();
	}

	@Override
	public void updateCart(CartUpdateDTO dto, Long userId) {

		BoundHashOperations<String, Object, Object> cardRedisOps = getCardRedisOps(userId);
		String o = (String) cardRedisOps.get(dto.getSkuId());

		CartRS cartRS = JSON.parseObject(o, CartRS.class);
		if(cartRS==null){
			throw new ServiceException("该商品状态不允许此操作");
		}
		if(dto.getCount()!=null){
			cartRS.setCount(dto.getCount());
		}
		if(dto.getChecked()!=null){
			cartRS.setChecked(dto.getChecked());
		}
		cardRedisOps.put(dto.getSkuId().toString(),JSON.toJSONString(cartRS));


	}

	@Override
	public void delCart(List<Long> keys,Long userId){
		BoundHashOperations<String, Object, Object> cardRedisOps = getCardRedisOps(userId);
		cardRedisOps.delete(keys.toArray());
	}

	@Resource
	private RedisTemplate<String, Object> redisTemplate;

	@Override
	public void addCart(Long skuId, Integer num,Long userId) {
		BoundHashOperations<String, Object, Object> cardRedisOps = getCardRedisOps(userId);
		String o = (String) cardRedisOps.get(skuId);
		if(StringUtil.isBlank(o)){
			//购物车数据中无此数据
			CartRS cartRS = skuService.getCartRSById(skuId);
			if(cartRS==null){
				throw new ServiceException("该商品状态不允许此操作");
			}
			cartRS.setAddDate(new Date());
			cartRS.setCount(num);
			cardRedisOps.put(skuId.toString(),JSON.toJSONString(cartRS));
		}else{
			CartRS cartRS = JSON.parseObject(o, CartRS.class);
			cartRS.setCount(cartRS.getCount()+num);
			cardRedisOps.put(skuId.toString(),JSON.toJSONString(cartRS));
		}



	}

	@Override
	public CartVO getCart(Long userId) {
		BoundHashOperations<String, Object, Object> cardRedisOps = getCardRedisOps(userId);
		List<Object> values = cardRedisOps.values();
		if( values == null){
			return null;
		}

		LinkedHashMap<Long,List<CartItemVO>> map = new LinkedHashMap<>();
		List<CartShopVO> cartShopVOS = new ArrayList<>(10);
		values.forEach(s -> {
			CartRS cs = JSON.parseObject((String) s,CartRS.class);
			if(!map.containsKey(cs.getShopId())){
				map.put(cs.getShopId(), new ArrayList<>(6));
				cartShopVOS.add(new CartShopVO(cs.getShopId(),cs.getShopName(),cs.getShopAvatar()));
			}
			CartItemVO cv = skuService.getCartDataBySkuId(cs.getSkuId());
			if(cv==null){
				cv=BeanUtil.copy(cs,CartItemVO.class);
				cv.setNowPrice(cs.getInitialPrice());
				cv.setAttrValue("请重新选择规格");
				cv.setChecked(false);
				cv.setStatus(0);
			}else{
				BeanUtil.copy(cs,cv);
			}
			map.get(cs.getShopId()).add(cv);
		});

		List<List<CartItemVO>> cartItems =new ArrayList<>(map.values());


		return new CartVO(cartItems,cartShopVOS);
	}

	private BoundHashOperations <String, Object, Object> getCardRedisOps(Long userId){
		return redisTemplate.boundHashOps(CART_KEY+userId );
	}

}
